/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.ieslaferreria.creditsintesis.controllers;

import com.jme.bounding.BoundingBox;
import com.jme.math.Vector3f;
import com.jmex.terrain.TerrainBlock;
import com.jmex.terrain.TerrainPage;
import edu.ieslaferreria.creditsintesis.logicgame.ActionStates;
import edu.ieslaferreria.creditsintesis.logicgame.ObjectWithStates;
import edu.ieslaferreria.creditsintesis.logicgame.actions.Action;
import edu.ieslaferreria.creditsintesis.logicgame.actions.ActionEvent;
import edu.ieslaferreria.creditsintesis.logicgame.actions.RmiEvent;
import edu.ieslaferreria.creditsintesis.logicgame.actions.Runner;
import edu.ieslaferreria.creditsintesis.rmi.GameEvent;
import java.util.HashMap;


/**
 * Controla los objetos con el terreno para que aparezcan en el suelo y no
 * vuelen por el aire y además crea los límites laterales.
 * @author Administrador
 */
public abstract class WorldController implements Runner{
    private CollisionController collisionController;
    private ActionStates actionstates;
    private Vector3f limits = new Vector3f();

    private HashMap<Object, Action> actionMap = new HashMap<Object, Action>();


    private TerrainPage terrainPage=null;
    private TerrainBlock terrainBlock = null;
    
    public WorldController(TerrainPage terrainPage, ActionStates actionStates){
        this.terrainPage=terrainPage;
        this.actionstates = actionStates;
    }
    public WorldController(TerrainBlock terrainBlock, ActionStates actionStates){
        this.terrainBlock=terrainBlock;
        this.actionstates = actionStates;
    }

    public WorldController(TerrainPage terrainPage, ActionStates actionStates, CollisionController col){
        this(terrainPage, actionStates);
        this.collisionController=col;
    }
    public WorldController(TerrainBlock terrainBlock, ActionStates actionStates, CollisionController col){
        this(terrainBlock, actionStates);
        this.collisionController=col;
    }

    /**
     * @return the terrainPage
     */
    public TerrainPage getTerrainPage() {
        return terrainPage;
    }


    /**
     * @return the terrainBlock
     */
    public TerrainBlock getTerrainBlock() {
        return terrainBlock;
    }
    public void setAction(Object id, Action action){
        this.actionMap.put(id, action);
    }

    public Action getAction(Object id){
        return this.actionMap.get(id);
    }
/**
 *
 * @param id del Objeto
 * @return
 */
    public Action removeAction(Object id){
        return this.actionMap.remove(id);
    }

    /**
     * @return the actionstates
     */
    public ActionStates getActionstates() {
        return actionstates;
    }

    /**
     * @param actionstates the actionstates to set
     */
    public void setActionstates(ActionStates actionstates) {
        this.actionstates = actionstates;
    }

    public abstract RmiEvent processAction(ActionEvent evt) ;

    /**
     * @return the collisionController
     */
    public CollisionController getCollisionController() {
        return collisionController;
    }

    /**
     * @param collisionController the collisionController to set
     */
    public void setCollisionController(CollisionController collisionController) {
        this.collisionController = collisionController;
    }
    /**
     * @param terrainPage the terrainPage to set
     */
    public void setTerrainPage(TerrainPage terrainPage) {
        this.terrainPage = terrainPage;
    }

    /**
     * @param terrainBlock the terrainBlock to set
     */
    public void setTerrainBlock(TerrainBlock terrainBlock) {
        this.terrainBlock = terrainBlock;
    }

    public void levelingWithTerrain(ObjectWithStates objw){
            if(getTerrainBlock()==null){
                float characterMinHeight = getTerrainPage().getHeight(objw.getLocalTranslation())+
                        objw.getOffsetPosition();
                if (!Float.isInfinite(characterMinHeight) && !Float.isNaN(characterMinHeight)) {
                        objw.getLocalTranslation().y = characterMinHeight;
                }
            }else{
                float characterMinHeight = getTerrainBlock().getHeight(objw.getLocalTranslation())+
                        objw.getOffsetPosition();
                if (!Float.isInfinite(characterMinHeight) && !Float.isNaN(characterMinHeight)) {
                        objw.getLocalTranslation().y = characterMinHeight;
                }
            }
            System.out.println(" Altura " + objw.getName() + " : " + objw.getOffsetPosition());

    }
   /**
     * @return the limits
     */
    public Vector3f getLimits() {
        return limits;
    }

    /**
     * @param limits the limits to set
     */
    public void setLimits(Vector3f limits) {
        this.limits = limits;
    }

}
